﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace WikcLib.Http
{
    public class Get
    {
        /// <summary>
        /// 测代理
        /// </summary>
        /// <param name="objProxy">代理</param>
        /// <returns></returns>
        public bool TestProxy(Proxy objProxy)
        {
            Get objGet = new Get();
            string strMimi = "";
            return objGet.RequestUrlToStringByProxy("http://baidu.com", "gb2312", ref strMimi, objProxy);
        }

        /// <summary>
        /// 请求路径返回源代码
        /// </summary>
        /// <param name="sUrl">请求地址</param>
        /// <param name="sCoding">编码格式</param>
        /// <param name="sMsg"></param>
        /// <returns></returns>
        public bool RequestUrlToString(string sUrl, string sCoding, ref string sMsg)
        {
            try
            {
                HttpWebRequest request = CreateWebRequest(sUrl);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();//获得回应的数据流
                //将数据流转成 String 
                sMsg = new StreamReader(stream, System.Text.Encoding.GetEncoding(sCoding)).ReadToEnd();
                return true;
            }
            catch (Exception ex)
            {
                sMsg = "" + ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 请求图片路径保存图片
        /// </summary>
        /// <param name="sImageUrl">图片路径</param>
        /// <param name="sFullFileName">保存图片的完整路径</param>
        /// <param name="sMsg"></param>
        /// <returns></returns>
        public bool RequestUrlToImage(string sImageUrl, string sFullFileName, ref string sMsg)
        {

            try
            {
                WebClient wc = new WebClient();
                wc.DownloadFile(sImageUrl, sFullFileName);
                sMsg = "成功保存图片到：" + sFullFileName;
                return true;
            }
            catch (Exception ex)
            {
                sMsg = "" + ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 使用代理请求
        /// </summary>
        /// <param name="sUrl"></param>
        /// <param name="sCoding"></param>
        /// <param name="sMsg"></param>
        /// <param name="proxy"></param>
        /// <returns></returns>
        public bool RequestUrlToStringByProxy(string sUrl, string sCoding, ref string sMsg, Proxy proxy)
        {
            try
            {
                HttpWebRequest request = CreateWebRequest(sUrl, proxy);
                request.Timeout = 1000;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream stream = response.GetResponseStream();//获得回应的数据流
                //将数据流转成 String 
                sMsg = new StreamReader(stream, System.Text.Encoding.GetEncoding(sCoding)).ReadToEnd();
                return true;
            }
            catch (Exception ex)
            {
                sMsg = "" + ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 创建请求对象 有代理
        /// </summary>
        /// <param name="sUrl"></param>
        /// <param name="proxy"></param>
        /// <returns></returns>
        private HttpWebRequest CreateWebRequest(string sUrl, Proxy proxy)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
            if (proxy != null)
            {
                string _sHost = proxy.ProxyAddress.TrimEnd('/');
                if (_sHost.ToUpper().IndexOf("HTTP://") == -1)
                {
                    _sHost = "http://" + _sHost;
                }
                WebProxy webProxy = new WebProxy();
                webProxy.Address = new Uri(_sHost.TrimEnd('/') + ":" + proxy.ProxyPort);
                if (!string.IsNullOrEmpty(proxy.ProxyAccount) && !string.IsNullOrEmpty(proxy.ProxyPassWord))
                {
                    webProxy.Credentials = new NetworkCredential(proxy.ProxyAccount, proxy.ProxyPassWord);
                    request.UseDefaultCredentials = true;
                }


                request.Proxy = webProxy;
            }
            return request;
        }

        /// <summary>
        /// 创建请求对象 无代理
        /// </summary>
        /// <param name="sUrl"></param>
        /// <returns></returns>
        private HttpWebRequest CreateWebRequest(string sUrl)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sUrl);
            return request;

        }

        /// <summary>
        /// 返回整个页面流
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public Stream GetStream(string url, Encoding encoding)
        {
            //初始化新的request对象
            HttpWebRequest webRequest = CreateWebRequest(url);
            //返回Internet资源的相应
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            //获取流
            Stream stream = webResponse.GetResponseStream();
            return stream;
        }
    }
}
